package cn.edu.lingnan.service.impl;

import cn.edu.lingnan.domain.dto.CreateAccount;
import cn.edu.lingnan.domain.pojo.Logs;
import cn.edu.lingnan.domain.pojo.Receipts;
import cn.edu.lingnan.domain.vo.AccountVo;
import cn.edu.lingnan.domain.vo.ManageUserVo;
import cn.edu.lingnan.mapper.TellerMapper;
import cn.edu.lingnan.service.TellerService;
import cn.edu.lingnan.utils.SnowflakeIdUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
@RequiredArgsConstructor
public class TellerServiceImpl implements TellerService {

    private final TellerMapper tellerMapper;

    @Override
    public List<AccountVo> queryAllAccount() {
        return   tellerMapper.queryAllAccount();
    }

    @Override
    public void updateAccountStatus(Integer accountId, String status) {
        tellerMapper.updateAccountStatus(accountId, status);
    }

    @Override
    public void openAccount(CreateAccount createAccount) {
        createAccount.setAccountNumber(SnowflakeIdUtil.nextId());
        // 获取当前时间并格式化
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        createAccount.setCreateTime(now.format(formatter)); // 设置创建时间
        tellerMapper.openAccount(createAccount);
    }

    @Override
    @Transactional
    public void deposit(Integer accountId, Double balance) {
        tellerMapper.deposit(accountId, balance);
        // 执行存款操作



        // 记录日志
        Logs logs = new Logs();
        logs.setOperationType("存款");
        logs.setDetails("柜员为账号:"+accountId+"存入"+balance+"元人民币");
         tellerMapper.insertLogs(logs);

        // 记录账单
        Receipts receipts = new Receipts();
        receipts.setAccountId( accountId);
        receipts.setBalance( balance);
        receipts.setOperationType(" 存款");
        receipts.setContent( "柜员为账号:"+accountId+"存入"+balance+"元人民币");

        // 获取当前时间并格式化
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        receipts.setCreateTime(now.format(formatter)); // 设置创建时间

        tellerMapper.insertReceipts(receipts);

    }

    @Override
    public void withdraw(Integer accountId, Double balance) {
        tellerMapper.withdraw(accountId, balance);
        // 执行取款操作

        // 记录日志
        Logs logs = new Logs();
        logs.setOperationType("取款");
        logs.setDetails("柜员为账号:"+accountId+"取出"+balance+"元人民币");
        tellerMapper.insertLogs(logs);

        // 记录账单
        Receipts receipts = new Receipts();
        receipts.setAccountId( accountId);
        receipts.setBalance( balance);
        receipts.setOperationType(" 取款");
        receipts.setContent( "柜员为账号:"+accountId+"取出"+balance+"元人民币");

        // 获取当前时间并格式化
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        receipts.setCreateTime(now.format(formatter)); // 设置创建时间

        tellerMapper.insertReceipts(receipts);
    }

    @Override
    public List<ManageUserVo> queryUserInfo() {
        return       tellerMapper.queryUserInfo();
    }
}
